require('./PageIndex.styl');

import reactMixin from 'react-mixin';
import Actions from './actions';
import Store from './store';
import {Control} from "react-keeper";
import {Field, Grid, Group, Icon, Slide} from 'saltui';
import Reflux from 'reflux';
import {PropTypes} from "prop-types";
import {Component} from "react";

class Index extends Component {

  constructor(props) {
    super(props);
  }

  notice() {
    dd.device.notification.alert({
      title: "提示",
      message: "敬请期待",
      buttonName: "确定",
      onSuccess: function (data) {
      },
      onFail: function (err) {
      }
    });
  }

  goCast(obj) {
    Control.go(obj.route);
  }

  handleRwRoute(obj) {
    let t = this;
    switch (obj.id) {
      case "db":
        dd.biz.util.openLink({
          url: obj.route,
          onSuccess: function (result) {

          },
          onFail: function (err) {
            alert(JSON.stringify(err));
          }
        });
        break;
      case "zsgc":
        dd.biz.util.openLink({
          url: obj.route,
        });
        //Control.go(obj.route);
        break;
      case 'nbtz':
        dd.biz.util.openLink({
          url: obj.route,
          onSuccess: function (result) {

          },
          onFail: function (err) {
            alert(JSON.stringify(err));
          }
        });
        break;
      case "notice":
        t.notice();
        break;
      default:
        break;
    }
  }

  componentDidMount() {

    this.timer = setInterval(
      () => {
        Actions.fetchDbsx();
      },
      20000
    );

  }

  componentWillMount() {
    Actions.initMenu();
    Actions.fetchDbsx();
    Actions.messageCount();
    Actions.engCount();
  }

  handleRoute(obj) {
    switch (obj.id) {
      case "yz":
        dd.biz.util.scan({
          type: 'qrCode',//type为qrCode或者barCode
          onSuccess: function (data) {
            dd.biz.util.openLink({
              url: data.text,
              onSuccess: function (result) {

              },
              onFail: function (err) {
                alert(JSON.stringify(err));
              }
            });
          },
          onFail: function (err) {
          }
        });
        break;

      case "zljd":
//        Control.go(obj.route);
        dd.biz.util.openLink({
          url: obj.route,
          onSuccess: function (result) {

          },
          onFail: function (err) {
            alert(JSON.stringify(err));
          }
        });
        break;
      case "aqjd":
//        Control.go(obj.route);
        dd.biz.util.openLink({
          url: obj.route,
          onSuccess: function (result) {

          },
          onFail: function (err) {
            alert(JSON.stringify(err));
          }
        });
        break;
      default:
        window.location = obj.route;
        break;
    }
  }

  add() {
    const t = this;

  }

  render() {
    let t = this;
    return (
      <div className="index">
        <Slide showNav auto height="135">
          {
            this.state.index.slideImgList.map((item, index) => (
              <div
                key={index}
                style={{
                  backgroundImage: `url(${item.img})`,
                  backgroundRepeat: 'no-repeat',
                  backgroundPosition: 'center',
                  backgroundSize: 'cover',
                }}
              />
            ))
          }
        </Slide>
        <Group>
          <Group.List>
            <Grid col={4} className="t-BCf" square={true}  >
              {
                this.state.index.rwList.map((item, index) => {
                  return (
                    /*onClick={t.goCast.bind(t, item)}*/
                    /*onClick={this.handleRwRoute.bind(t,item)}*/
                    <div key={index} onClick={this.handleRwRoute.bind(t,item)}  >
                      <div className="grid-contia">
                        <div className="grid-count">{item.value}</div>
                      </div>
                      <div className="grid-text">{item.text}</div>
                    </div>
                  )
                })}
            </Grid></Group.List>
          <div style={{height: '8px'}}/>
          <Group.List>
            <Field layout="h" label="功能模块"/>
            <Grid col={4} className="t-BCf" square={true} touchable={true}>
              {
                this.state.index.menuList.map((item, index) => {
                  return (
                    <div className="t-FBV t-FBAC t-FBJC home-grid-item t-TE" key={index}
                         onClick={t.handleRoute.bind(t, item)}>
                      <div className="home-grid-icon">
                        <img
                          src={item.img}
                          width="50"
                          height="50"/>
                      </div>
                      <div className="grid-text">{item.text}</div>
                    </div>
                  )
                })}
            </Grid>
          </Group.List>

          <div style={{height: '8px'}}/>
          <Group.List>
            <Field layout="h" label="新闻"/>
            <div className="iframe-div">
              <iframe src="http://www.yczaz.com/xydjgz/index.jhtml" width="100%" height="300"/>
            </div>
          </Group.List>
        </Group>

      </div>
    );
  }
}

Index.contextTypes = {
  router: PropTypes.object.isRequired
};

reactMixin.onClass(Index, Reflux.connect(Store, "index"));

export default Index;
